package org.jeecg.modules.friends.service.impl;

import static org.jeecg.modules.friends.handler.TradeOrderCreatePushHandler.NIANKA_ID;
import static org.jeecg.modules.friends.handler.TradeOrderCreatePushHandler.YUEKA_ID;
import static org.jeecg.modules.friends.handler.TradeOrderCreatePushHandler.ZHOUKA_ID;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.friends.dao.SystemConfigDAO;
import org.jeecg.modules.friends.dao.TradeOrderDAO;
import org.jeecg.modules.friends.model.SystemConfigDO;
import org.jeecg.modules.friends.model.TradeOrderDO;
import org.jeecg.modules.friends.service.VipUserService;
import org.jeecg.modules.friends.trade.dto.TradeOrderExtData;
import org.jeecg.modules.friends.user.entity.User;
import org.jeecg.modules.friends.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class TradeOrderServiceImpl implements org.jeecg.modules.friends.service.TradeOrderService {
    @Autowired
    private TradeOrderDAO tradeOrderDAO;

    @Autowired
    private SystemConfigDAO systemConfigDAO;

    @Autowired
    private VipUserService vipUserService;

    @Autowired
    private IUserService userService;

    @Override
    public void save(TradeOrderDO tradeOrderDO) {
        tradeOrderDAO.insert(tradeOrderDO);
    }

    @Override
    public TradeOrderDO queryByOid(String oid) {
        return tradeOrderDAO.queryByOid(oid);
    }

    @Override
    public void refundOrder(String oids) {
        TradeOrderDO tradeOrderDO = queryByOid(oids);
        if (tradeOrderDO != null && tradeOrderDO.getStatus().equals("TRADE_PAID")) {
            if (StringUtils.isNotEmpty(tradeOrderDO.getExtData())) {
                TradeOrderExtData tradeOrderExtData = JSON.parseObject(tradeOrderDO.getExtData(), TradeOrderExtData.class);
                if (StringUtils.isNotEmpty(tradeOrderExtData.getOrderSkuProperties())) {
                    JSONArray jsonArray = JSONArray.parseArray(tradeOrderExtData.getOrderSkuProperties());
                    SystemConfigDO systemConfigDO = systemConfigDAO.findOne();
                    if (Objects.nonNull(jsonArray) && jsonArray.size() > 0) {
                        int ticket = Objects.isNull(systemConfigDO) ? 7 : systemConfigDO.getTicket();
                        JSONObject jo = jsonArray.getJSONObject(0);
                        User user = userService.findById(tradeOrderExtData.getUserId());
                        if (Objects.nonNull(user)) {
                            if (jo.getInteger("v_id") == ZHOUKA_ID.intValue()) {
                                vipUserService.minusVipUser(user, 1, ticket, 7 * 24 * 3600L * 1000);
                            }
                            if (jo.getInteger("v_id") == YUEKA_ID.intValue()) {
                                vipUserService.minusVipUser(user, 2, ticket, 30 * 24 * 3600L * 1000);
                            }
                            if (jo.getInteger("v_id") == NIANKA_ID.intValue()) {
                                vipUserService.minusVipUser(user, 3, ticket, 365 * 24 * 3600L * 1000);
                            }
                            log.info("用户userId:{}, 退款成功，扣除会员", user.getId());
                            return;
                        }
                    }
                }
            }
        }
    }
}